<?php echo $this->form; ?>

<script>
	$(document).ready(function() {
    	$('.esculateEditor').tinymce({
            //'apply_source_formatting': 1,
            'script_url' : '/js/tiny_mce/tiny_mce.js',
            'theme'      : 'simple',
            'plugins'    : 'pagebreak,',
            'width'      : '730',
            'height'     : '220'
        });
	});

    function split(val) 
    {
    	return val.split( /,\s*/ );
    }
    
    function extractLast(term) 
    {
    	return split(term).pop();
    }

    $(document).ready(function() {
        $('#user')
            .bind('keydown', function(event) {
                if(event.keyCode === $.ui.keyCode.TAB && $(this).data('autocomplete').menu.active) 
                {
                    event.preventDefault();
                }
            })
            .autocomplete({
                source: function(request, response) {
                    $.getJSON('/backoffice/users/search', {
                        term: extractLast(request.term)
                    }, response );
				},
                'focus': function() {
                    // prevent value inserted on focus
                    return false;
                },
                'search': function() {
                    // custom minLength
                    var term = extractLast(this.value);

                    if (term.length < 2) 
                    {
                        return false;
                    }
                },
                'select': function(event, ui) {
                    var terms = split(this.value);

                    // remove the current input
                    terms.pop();

                    if(ui.item.isNewContact != true)
                    {
                        // add the selected item
                        terms.push(ui.item.name + " <" + ui.item.email + ">");
                    }
                    
                    // add placeholder to get the comma-and-space at the end
                    terms.push("");
                    this.value = terms.join(", ");
                    
                    return false;
                }
            })
            .data('autocomplete')._renderItem = function(ul, item){
                return $('<li></li>').data('item.autocomplete', item)
                                     //.append('<a style="color: #444 !important;"><strong>'+item.name+'</strong><br />'+(item.isNewContact == true ? '&nbsp;' : item.email)+'</a>')
                                     .append('<a style="color: #444 !important;"><table cellpadding="0" cellspacing="0"><tr><td rowspan="2"><img src="/images/userbox.png" title="User" /></td><td><strong>'+item.name+'</strong></td></tr><tr><td>'+item.email+'</td></tr></table></a>')
                                     .appendTo(ul);
            };
    });
</script>